package com.sun.util;

import java.util.*;
import java.io.*;

import com.sun.model.Student;

public class DealFile {

	public static Vector<Student> getStudents(String fileName)
	{
		Vector<Student> students = new Vector<Student>();
		FileReader fr = null;
		BufferedReader br = null;
		try {
			 fr = new FileReader(fileName);
			 br = new BufferedReader(fr);
			 String line = null;
			 while((line=br.readLine())!=null)
			 {
				 String [] str = line.split("\\s");
				 Student s = new Student();
				 s.setId(str[0]);
				 s.setName(str[1]);
				 students.add(s);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			try {
				if(br!=null) br.close();
				if(fr!=null) fr.close();			
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		
		return students;
	}

	public static String[] getClasses()
	{
		File file = new File("data/classdata");
		String [] classes = file.list();
		return classes;
	}

	public static Vector<String> getCourse()
	{
		Vector<String> courses = new Vector<String>();
		
		FileReader fr = null;
		BufferedReader br = null;
		try {
			 fr = new FileReader("data/course.txt");
			 br = new BufferedReader(fr);
			 String line = null;
			 while((line=br.readLine())!=null)
			 {
				 courses.add(line);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			try {
				if(br!=null) br.close();
				if(fr!=null) fr.close();			
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		return courses;
	}

	public static Vector<Student> getScore(String fileName)
	{
		Vector<Student> students = new Vector<Student>();
		FileReader fr = null;
		BufferedReader br = null;
		try {
			 fr = new FileReader(fileName);
			 br = new BufferedReader(fr);
			 String line = null;
			 while((line=br.readLine())!=null)
			 {
				 String [] str = line.split("\\s");
				 Student s = new Student();
				 s.setId(str[0]);
				 s.setName(str[1]);
				 students.add(s);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			try {
				if(br!=null) br.close();
				if(fr!=null) fr.close();			
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		
		return students;
	}
	
	public static Object[][] getStudentData(String fileName)
	{
		Vector<Student> students = getStudents(fileName);
		Object [][] ob = new Object[students.size()][3];
		for(int i=0; i<students.size();i++)
		{
			Student s = new Student();
			s = students.get(i);
			ob[i][0]=s.getId();
			ob[i][1]=s.getName();
			ob[i][2]=null;
		}
		return ob;
	}
	
	public static void wObject(String fileName,Student[]students)
	{
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try {
				fos = new FileOutputStream(new File(fileName));
				oos = new ObjectOutputStream(fos);
				for(int i=0; i<students.length; i++)
				{
					Student s = new Student();
					s = students[i];
					oos.writeObject(s);
				}

				oos.writeObject(null);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
				try {
					 if(oos!=null) oos.close();
					 if(fos!=null) fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	public static Object[][] rObject(String fileName)
	{
		
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		ArrayList<Student> s =null;
		try {
			fis = new FileInputStream(new File(fileName));
			ois = new ObjectInputStream(fis);
			s = new ArrayList<Student>();
			
			while(true)
			{
				Student ss = new Student();

				if((ss = (Student) ois.readObject())==null)
					break;
				s.add(ss);
			}
		} catch (Exception e) {
		//	e.printStackTrace();
		}
			Object[][]ob = new Object[s.size()][3];
			for(int i=0; i<s.size();i++)
			{
				Student student = s.get(i);
				ob[i][0]=student.getId();
				ob[i][1]=student.getName();
				ob[i][2]=student.getScore();
			}
		return ob; 
	}
	
	public static int[] getCount(String fileName)
	{
		int [] a = {0,0,0,0,0};
		Object[][] s = new Object[rObject(fileName).length][3];
		s = rObject(fileName);
		for(int i=0; i<s.length;i++)
		{
			if((double)s[i][2]<60.0)
				a[0]++;	
			else if((double)s[i][2]<70.0)
				a[1]++;	
			else if((double)s[i][2]<80)
				a[2]++;	
			else if((double)s[i][2]<90)
				a[3]++;	
			else 
				a[4]++;	
		}
		return a;
	}
}
